package com.lsc.record.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lsc.record.core.entity.DmListview;
import com.lsc.record.core.entity.DmListviewItem;
import com.lsc.record.core.entity.DmType;
import com.lsc.record.core.entity.DmTypeAttr;
import com.lsc.record.core.mapper.DmListviewItemMapper;
import com.lsc.record.core.mapper.DmListviewMapper;
import com.lsc.record.core.mapper.DmTypeMapper;
import com.lsc.record.core.service.ListViewServcie;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

@Service
public class ListViewServcieImpl extends ServiceImpl<DmListviewMapper, DmListview> implements ListViewServcie {

	@Resource
	private DmTypeMapper typeMapper;
	
	@Resource
	private DmListviewItemMapper lvMapper;
	
	@Override
	public DmListview initListViewByType(String typeId) {
		QueryWrapper queryWrapper=QueryWrapper.create().eq("type_id", typeId);
		long count = this.count(queryWrapper);
		if(count>0) {
			return null;
		}
		DmType type = typeMapper.selectOneWithRelationsById(typeId);
		
		DmListview listview = new DmListview();
		listview.setName(type.getName());
		listview.setTypeId(type.getId());
		listview.setConditionStr("");
		
		this.save(listview);
		String listId = listview.getId();
		List<DmTypeAttr> itemList = type.getItems();
		int sortIndex = 0;
		
		List<DmListviewItem> insertList = new ArrayList<>();
		for (DmTypeAttr attr : itemList) {
			String name = attr.getName().toLowerCase();
			if(name.contains("delete") || name.equals("version")) {
				continue;
			}
			DmListviewItem item = new DmListviewItem();
			item.setName(attr.getName());
			item.setLabel(attr.getTitle());
			item.setListId(listId);
			item.setSortIndex(sortIndex);
			insertList.add(item);
			sortIndex++;
		}
		
		for (DmListviewItem lvi : insertList) {
			this.lvMapper.insert(lvi, true);
		}
		return this.mapper.selectOneWithRelationsById(listId);
	}

	@Override
	public DmListview initListView(DmType type) {
		List<DmListview> list = this.mapper.selectListByQuery(QueryWrapper.create().eq("type_id", type.getId()));
		if(!list.isEmpty()){
			return null;
		}
		DmListview listview = new DmListview();
		listview.setName(type.getName());
		listview.setTypeId(type.getId());
		listview.setConditionStr("");

		this.save(listview);
		String listId = listview.getId();
		List<DmTypeAttr> itemList = type.getItems();
		int sortIndex = 0;

		List<DmListviewItem> insertList = new ArrayList<>();
		for (DmTypeAttr attr : itemList) {
			String name = attr.getName().toLowerCase();
			if(name.contains("delete") || name.equals("version")) {
				continue;
			}
			DmListviewItem item = new DmListviewItem();
			item.setName(attr.getName());
			item.setLabel(attr.getTitle());
			item.setListId(listId);
			item.setSortIndex(sortIndex);
			insertList.add(item);
			sortIndex++;
		}

		for (DmListviewItem lvi : insertList) {
			this.lvMapper.insert(lvi, true);
		}
		return this.mapper.selectOneWithRelationsById(listId);
	}

}
